package com.jy.modules.cims.component.jieyue;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.open.api.enums.EntryResponseEnum;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.ExceptionUtils;
import com.jy.modules.cims.common.sysUtils.SearchParamUtil;
import com.jy.modules.cims.component.api.IJieYueSend;
import com.jy.modules.cims.data.dao.LoanDao;
import com.jy.modules.cims.data.dto.LoanDetailDto;
import com.jy.modules.cims.data.interact.api.JieYueResponseData;
import com.jy.modules.cims.data.interact.api.request.RequestAttachSearchDTO;
import com.jy.modules.cims.data.interact.api.response.ResponseAttachSearchDTO;

@Service("com.jy.modules.cims.component.jieyue.CSynProcessImagesSignImpl")
public class CSynProcessImagesSignImpl{

	private static final Logger logger = LoggerFactory.getLogger(CSynProcessImagesSignImpl.class);
	
	
	@Autowired
	@Qualifier("jieYueSendService")
	private IJieYueSend jieYueSend;
	
	@Autowired
	private LoanDao loanDao;
	/**
	 * @Description 根据机构编码，查询系统需要同步更新放款状态的贷款明细。 调用外围系统接口查询放款结果。更新系统贷款明细的贷款状态。
	 * @param cooperorgCode
	 *            机构编码
	 * @throws Exception
	 * @author zhangmi
	 * @date 2016-12-20
	 */
	public List<ResponseAttachSearchDTO> doSign(String cooperorgCode) throws Exception {

		logger.info("查询附件信息开始>>");
		List<ResponseAttachSearchDTO> result = new ArrayList<ResponseAttachSearchDTO>();
		
		LoanDetailDto searchDto = new LoanDetailDto();
		searchDto.setCooperorgCode(cooperorgCode);//合作机构
		searchDto.setLoanStatus(CimsConstant.LOAN_FUNDOUT_WIN);//贷款状态-放款成功
		searchDto.setLoanEachStatus(CimsConstant.LOAN_EACH_STATUS_CORE);//贷款交互状态-放款查询完毕的
		List<LoanDetailDto> list = loanDao.searchLoanByCooCodeAndSatus(SearchParamUtil.getSearchParam(searchDto));
		if(CollectionsUtils.isNotEmpty(list)){
			String contractNo = null;
			for (LoanDetailDto loanDetailDto : list) {
				contractNo = loanDetailDto.getContractNo();
				try {
					//调用查询附件信息接口
					JieYueResponseData responseData = jieYueSend.searchLoanSignature(cooperorgCode, new RequestAttachSearchDTO(contractNo));
					if(responseData!=null){
						if(EntryResponseEnum.SUCESS21001.getCode().equals(responseData.getRetCode())){
							result.add((ResponseAttachSearchDTO)responseData.getData());
						}else{
							logger.info("合作机构【{}】，查询附件信息接口异常编码【{}】，异常描述【{}】，合同号【{}】！",cooperorgCode,responseData.getRetCode(),responseData.getMessage(),contractNo);
						}
					}else{
						logger.info("合作机构【{}】，查询附件信息接口，返回数据为空！",cooperorgCode);
					}
				} catch (Exception e) {
					logger.error("合作机构【{}】，合同号【{}】，报错{}！",cooperorgCode,contractNo,ExceptionUtils.getStackTraceAsString(e));
				}
				
			}
		}
		logger.info("查询附件信息结束>>");
		return result;
	}
}
